import useSys from 'src/pinia/modules/sys';
import useOrg from 'src/pinia/modules/org';
import {storage,defineStore,keys} from 'src/pinia/common';

export default defineStore('user', {
    'state': () => ({
        'userInfo': {},
        'menus': [],
        'keepRoutes':[]
    }),
    'actions': {
        logout() {
            const sys = useSys();
            const org = useOrg();
            this.$reset();
            sys.$reset();
            org.$reset();
            sessionStorage.clear();
        },
        addRoute(opt) {
            const exist = this.keepRoutes.filter(keepRoute => keepRoute.routerName === opt.routerName).length;
            if (!exist) { this.keepRoutes.push(opt); }
        },
        delRoute(opt) {
            this.keepRoutes.splice(this.keepRoutes.findIndex(keepRoute => keepRoute.routerName === opt.routerName),1);
        },
        clearRoute() {
            this.keepRoutes = [];
        }
    },
    'persist': {
        'enabled': true,
        // 修改存储中使用的键名称，默认为当前 Store的 id
        'key': keys.user,
        // 修改为 sessionStorage，默认为 localStorage
        storage
    // 部分持久化状态的点符号路径数组，[]意味着没有状态被持久化(默认为undefined，持久化整个状态)
    //'paths': ['userInfo.token']
    }
});
